package cn.itcast.item.controller;


import cn.itcast.hmall.dto.common.PageDTO;
import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.dto.item.SearchItemDTO;
import cn.itcast.hmall.pojo.item.Item;
import cn.itcast.item.mapper.ItemMapper;
import cn.itcast.item.service.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/item")
@Slf4j
public class ItemController {
    @Autowired
    private ItemService itemService;

    //分页查询
    @PostMapping("/list")
    public PageDTO<Item> list(@RequestBody SearchItemDTO dto){
        return itemService.getlist(dto);
    }

    //添加
    @PostMapping
    public ResultDTO add(@RequestBody Item item){
        return itemService.add(item);
    }
    //商品上架与下架
    @PutMapping("/status/{id}/{status}")
    public ResultDTO UpAndDown(@PathVariable String id,
                               @PathVariable String status){
        return itemService.UpAndDown(id,status);
    }
    //修改
    @PutMapping
    public ResultDTO update(@RequestBody Item item){
        return itemService.updata(item);
    }
    //删除
    @DeleteMapping("{id}")
    public ResultDTO deleteById(@PathVariable("id") String id){
        return ResultDTO.ok(itemService.removeById(id));
    }
    //根据id查询商品
    @GetMapping("/{id}")
    public Item selectById(@PathVariable("id") String id){
        return itemService.getById(id);
    }










}
